package com.yangcangclub.message.utils;

import com.cloopen.rest.sdk.CCPRestSDK;
import com.yangcangclub.commons.utils.EncryptUtils;
import com.yangcangclub.commons.utils.ObjectUtils;
import com.yangcangclub.commons.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;

/**
 * 云通讯短信发送工具类
 */
public class SMSUtil {
    private static Logger logger = LoggerFactory.getLogger(SMSUtil.class);
    private static CCPRestSDK restAPI = new CCPRestSDK();
//
    static{
        restAPI.init("app.cloopen.com", "8883");
//        restAPI.setAccount("8a48b5514e04a574014e09da5ca30433", "5371edbf575f46b7bb4acb5456dd4be9");
        restAPI.setAccount("8aaf070857d1e1140157dabcda7c0695", "0ef47e5bd16b42b480f7cf2394d842f8");
//        restAPI.setAppId("8aaf070855e8e0c70155ec63fd740168");
        restAPI.setAppId("8aaf07085967081d0159672a7c400030");
    }



    public static boolean sendSMS(String phone, String tempId, String[] content) {

//        String rpcUrl = "http://api.synhey.com/json?key=6fnbpt&secret=TsYg1CUB&to=" + phone + "&text=" + "您的验证码是：" + content[0] + "。如非本人操作，请忽略本短信。";
//        Document doc = null;
//        try {
//            doc = Jsoup.connect(EncryptUtils.URLDecoder(rpcUrl)).ignoreContentType(true).timeout(3000).get();
//        } catch (IOException e) {
//            try {
//                doc = Jsoup.connect(EncryptUtils.URLDecoder(rpcUrl)).ignoreContentType(true).timeout(5000).get();
//            } catch (IOException e1) {
//            }
//        }
//        if (ObjectUtils.isNotNull(doc)) {
//            String body = doc.body().html();
//            SmsJson smsJson = JSON.parseObject(body, SmsJson.class);
//            if ("0".equals(smsJson.getStatus())) {
//                return true;
//            } else {
//                logger.error(" sms send fail statusCode={}, statusMsg={}", smsJson.getStatus(), smsJson.getMessageid());
//                return false;
//            }
//        }
//
//        return false;
//
//        phone=StringUtil.splitPhoneNumber(phone);

        HashMap<String, Object>   result = restAPI.sendTemplateSMS(phone,tempId ,content);
        if("000000".equals(result.get("statusCode"))){
            return true;
        }else{
            logger.error(" sms send fail statusCode={}, statusMsg={}",result.get("statusCode"),result.get("statusMsg"));
           return false;
        }
    }

    public static void main(String[] args) {
        sendSMS("15330059913","146991",new String[]{"1234","10"});
    }

}
